package com.sinder.campus_prophet.web.wxapi;

import cc.lyceum.api.thxy.jwgl.ThxyJwgl;
import com.sinder.campus_prophet.core.global.ResponseMessage;
import com.sinder.campus_prophet.service.general.WxApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * 微信基础应用接口
 * @author sinder
 * Date 2023/3/18/12:51
 **/
@RestController
@RequestMapping("wx_mini")
@Slf4j
public class WxApiController {
    private final WxApiService wxApiService;

    public WxApiController(WxApiService wxApiService) {
        this.wxApiService = wxApiService;
    }

    /**
     * 获取微信openId
     * @param code 微信用户授权码
     * @return ResponseMessage
     */
    @GetMapping("getWxOpenId")
    public ResponseMessage<Map<String, String>> getWxOpenId(String code) {
        Map<String, String> map = wxApiService.getWxOpenId(code);
        if (map.containsKey("error"))
            return ResponseMessage.createByErrorMessage(map.get("error"));
        else
            return ResponseMessage.createBySuccess(map);
    }

    /**
     * 获取accessToken
     * @return access_token
     */
    @GetMapping("getAccessToken")
    public ResponseMessage getAccessToken() {
        Map<String, String> data = wxApiService.getAccessToken();
        if (data.containsKey("error"))
            return ResponseMessage.createByErrorMessage(data.get("error"));
        else
            return ResponseMessage.createBySuccess(data);
    }
}
